package com.admin.action;

import com.admin.bean.Banner;
import com.admin.service.BannerAdminService;
import com.data.util.CheckDataUtils;
import com.data.util.ImageLookUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 客户端的banner管理
 *
 * @author:HingLo
 * @create 2017-09-26 14:30
 **/
@Controller
@RequestMapping("/bannerAdmin/admin/")
public class BannerAdminAction {
    @Autowired
    private BannerAdminService bannerAdminService;

    @GetMapping("inputBanner")
    public String inputBannerAdminAction(HttpServletRequest request) {
        List<?> list = bannerAdminService.findAllBannerTypeService();
        request.setAttribute("dataMap", list);
        return "/WEB-INF/admin/banner/banner";
    }

    /**
     * 进入到添加banner图界面
     *
     * @param request
     * @return
     */
    @GetMapping("addBanner")
    public String inputBannerAddAdminAction(HttpServletRequest request, String bool) {
        if (CheckDataUtils.stringUtilsID(bool)) {
            request.setAttribute("bool", bool);
        }


        return "/WEB-INF/admin/banner/addBanner";
    }

    /**
     * 添加banner图
     *
     * @param banner
     * @param request
     * @return
     */
    @PostMapping("submitBannerAdd")
    public String submitBannerAddAdminAction(@RequestParam("file") CommonsMultipartFile file, Banner banner, String date, HttpServletRequest request) {
        boolean bool = false;
        if (banner != null) {
            banner.setIcon(file.getBytes());
            if (CheckDataUtils.stringUtils(date)) {
                bool = bannerAdminService.submitBannerAddAdminService(banner, date);
            }

        }
        request.setAttribute("boolData", bool);
        return "/WEB-INF/admin/banner/addBanner";

    }


    /**
     * 删除banner图
     *
     * @param id
     * @return
     */
    @PostMapping("deleteBanner")
    @ResponseBody
    public String delBannerAdminAction(String id) {
        boolean bool;
        bool = bannerAdminService.delBannerAdminService(id);
        return String.valueOf(bool);
    }

    @GetMapping("lookBannerIcon")
    public void lookBannerIconAction(String id, HttpServletResponse response) {

        if (CheckDataUtils.stringUtilsID(id)) {
            Banner banner = bannerAdminService.lookBannerIconService(id);
            if (banner != null) {
                byte[] inputStream = banner.getIcon();
                ImageLookUtils.responseImage(inputStream, response);
            }
        }
    }


}
